﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace library
{
    public class p2pResult
    {
        public Cache<p2pResultItem> Terms = new Cache<p2pResultItem>(2000);

        public void AddTerm(p2pPost post, byte[] id)
        {
            Terms.Add(new p2pResultItem { Post = post, Id = id });
        }

        public ValueHits<p2pPost> RemoveDuplicates(List<ValueHits<p2pPost>> value, byte[] id)
        {
           var result = new ValueHits<p2pPost>();
            
            value.ForEach(x =>
            {
                lock(x)
                x.ForEach(y =>
                    {
                        lock(Terms)
                            if (Terms.Any(z =>
                                Addresses.Equals(y.HitValue.Address, z.CachedValue.Post.Address) && Addresses.Equals(z.CachedValue.Id, id)))
                            {
                                //y.HitValue.Content = null;
                            }
                            else
                            {
                                AddTerm(y.HitValue, id);
                            }

                        result.Add(y.HitValue, y.Hits);
                    });
            });

            return result;
        }
    }

    public class p2pResultItem
    {
        public p2pPost Post;

        public byte[] Id;

        public List<p2pPost> ReturnedContent = new List<p2pPost>();


    }
}
